# -*- coding： UTF-8 -*-
from util.math.MathUtil import transToPostExp,computePostExpValue

class Expression:

    def __init__(self,expression):
        self.expression = expression
        self.postExp = transToPostExp(self.expression)
        self.hash = self.computeHashCode()

    def computeHashCode(self):
        hashCode = []
        stack = []
        postExp = self.postExp
        for item in postExp:
            if type(item) == int or "/" in item:
                stack.append(item)
            elif item == '#':
                stack.append(item)
            else:
                hashCode.append(item)
                top = stack.pop()
                if top != '#':
                    hashCode.append(top)
                top = stack.pop()
                if top != '#':
                    hashCode.append(top)
                stack.append('#')
        return hashCode


    def getPostExp(self):
        return self.postExp

    def getExpression(self):
        return self.expression

    def getHashCode(self):
        return  self.hash

    def getStringExpression(self):
        expStr = ''
        for item in self.expression:expStr +=' '+str(item)
        return  expStr

    def isNegative(self):
        r = computePostExpValue(self.postExp)
        if type(r) is list:
            return True if (r[0] < 0 or r[1] < 0) else False
        else:
            return True if r < 0 else False